package com.zsr.rabbitmq.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * 延迟队列配置文件
 *
 * @author zsr
 * @date 2021/8/11 17:20
 */
@Configuration
public class TtlQueueConfig {
    //正常交换机
    public static final String X_EXCHANGE = "X";
    //TTL为10S的队列
    public static final String QUEUE_A = "QA";
    //TTL为40S的队列
    public static final String QUEUE_B = "QB";
    //死信交换机
    public static final String Y_DEAD_LETTER_EXCHANGE = "Y";
    //死信队列
    public static final String DEAD_LETTER_QUEUE = "QD";

    //声明普通交换机
    @Bean("xExchange")
    public DirectExchange xExchange() {
        return new DirectExchange(X_EXCHANGE);
    }

    //声明死信交换机
    @Bean("yExchange")
    public DirectExchange yExchange() {
        return new DirectExchange(Y_DEAD_LETTER_EXCHANGE);
    }

    //声明普通队列 10s（有TTL,没有消费者,到达TTL后进入死信交换机）
    @Bean("queueA")
    public Queue queueA() {
        Map<String, Object> arguments = new HashMap<>();
        //设置死信交换机
        arguments.put("x-dead-letter-exchange", Y_DEAD_LETTER_EXCHANGE);
        //设置死信RoutingKey 不设置的话延迟队列中没有消息
        arguments.put("x-dead-letter-routing-key", "YD");
        //设置TTL  单位ms
        arguments.put("x-message-ttl", 10000);
        return QueueBuilder.durable(QUEUE_A).withArguments(arguments).build();
    }

    //声明普通队列 40s
    @Bean("queueB")
    public Queue queueB() {
        Map<String, Object> arguments = new HashMap<>();
        //设置死信交换机
        arguments.put("x-dead-letter-exchange", Y_DEAD_LETTER_EXCHANGE);
        //设置死信RoutingKey(出现dead letter之后将dead letter重新按照指定的routing-key发送)
        //如果设置死信会以路由键YD转发到 死信交换机，如果没设默认为消息发送到本队列时用的routing key
        //交换器根据消息携带的路由键，来决定消息交给哪个队列。交换机根据这个绑定规则来交给队列以后，消费者就可以连接队列取出消息。
        /**
         * For example, if you publish a message to an exchange with routing key foo,
         * and that message is dead-lettered, it will be published to its dead letter exchange with routing key foo.
         * If the queue the message originally landed on had been declared with x-dead-letter-routing-key set to bar,
         * then the message will be published to its dead letter exchange with routing key bar
         */
        arguments.put("x-dead-letter-routing-key", "YD");
        //设置TTL  单位ms
        arguments.put("x-message-ttl", 40000);
        return QueueBuilder.durable(QUEUE_B).withArguments(arguments).build();
    }

    //声明死信队列
    @Bean("queueD")
    public Queue queueD() {
        return QueueBuilder.durable(DEAD_LETTER_QUEUE).build();
    }

    //绑定（exchange 和 queue 之间的桥梁，它告诉我们 exchange 和那个队列进行了绑定关系）
    @Bean
    public Binding queueABindingX(@Qualifier("queueA") Queue queueA,
                                  @Qualifier("xExchange") DirectExchange xExchange) {
        return BindingBuilder.bind(queueA).to(xExchange).with("XA");
    }

    @Bean
    public Binding queueBBindingX(@Qualifier("queueB") Queue queueB,
                                  @Qualifier("xExchange") DirectExchange xExchange) {
        return BindingBuilder.bind(queueB).to(xExchange).with("XB");
    }

    @Bean
    public Binding queueDBindingY(@Qualifier("queueD") Queue queueD,
                                  @Qualifier("yExchange") DirectExchange yExchange) {
        return BindingBuilder.bind(queueD).to(yExchange).with("YD");
    }
}
